package com.scheduled.task.utils.task;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.scheduled.task.entity.BusinessFinance;
import com.scheduled.task.entity.BusinessOriginal;
import com.scheduled.task.entity.SysUser;
import com.scheduled.task.mapper.BusinessMapper;
import com.scheduled.task.mapper.UserMapper;
import com.scheduled.task.utils.HttpClientServer;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.DataInput;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author JiangHaoHuan
 * @Date 2024/7/16 17:26:02
 * @Version 1.0
 */
@Component
@RequiredArgsConstructor
@Slf4j
@Configuration
public class tasks {

    private final HttpClientServer httpClientServer;

    private final UserMapper userMapper;

    private final BusinessMapper businessMapper;

    @Value("${busniess.url}")
    private String url;

    @XxlJob("lessExpireSchoolFileJob")
    public ReturnT<String> lessExpireSchoolFileJob(String param) {

        XxlJobHelper.log("任务开始");
        String jobParam = XxlJobHelper.getJobParam();
        System.out.println("dsdsdsdsd:"+jobParam);
        SysUser sysUser = userMapper.selectByName(jobParam);
        System.out.println("dsdsdsdsd:"+sysUser);
        log.error("dsdsdsdsd:"+sysUser);
        JSONObject query = new JSONObject();
//        {"login_type": 1, "username": "15974851441", "password": "Nbhyzx@123", "mobile": "", "code": "" , "login_source"
//:
//            "PC"}
        query.put("login_type",1);
        query.put("username","17368089015");
        query.put("password","Nbhyzx@123");
        query.put("mobile","");
        query.put("code","");
        query.put("login_source","PC");
        int pageSize=100;
        int pageNum=1;
        try {
            JSONObject result = httpClientServer.postHttp(url+"/api/admin/v1/auth/manager/login", query);
            System.out.println("dsdsdsdsd:"+result);
            JSONObject  date= JSONObject.parseObject(result.get("data").toString());
            String token = String.valueOf(date.get("access_token"));
            log.info("dsdsdsdsd:"+result);

            isnetEndAll(pageNum,pageSize,token);

            XxlJobHelper.log("任务结束");
            System.out.println("dsdsdsd");
        }catch (Exception e){
            log.error("lessExpireSchoolFileJob错误信息 {}",e);
        }

        return ReturnT.SUCCESS;
    }

    private void isnetEndAll(int pageNum, int pageSize, String token) {
        try {
            JSONObject gg =httpClientServer.getHttp(url+"/api/contaract/v1/settlement/settlementList/?search" +
                    "=&page_num="+pageNum+"&page_size="+pageSize+"&disable_page=false",token);
//            if(gg.get){
//
//            }
            List<BusinessFinance> businessFinance =null;
            BusinessOriginal businessOriginal = JSONObject.parseObject( gg.toJSONString(), BusinessOriginal.class);
            int total = 0;
            if(businessOriginal.getCode() == 0){
                businessFinance =
                        Optional.ofNullable(businessOriginal).map(BusinessOriginal::getData).map(BusinessOriginal.allData::getList).orElse(null);
                total =
                        Optional.ofNullable(businessOriginal).map(BusinessOriginal::getData).map(BusinessOriginal.allData::getTotal)
                                .orElse(0);
            }
            XxlJobHelper.log("任务开始页数：" +pageNum);
            if(total >= pageNum*pageSize){
                pageNum+=1;
            }else {
                total=0;
            }

            if(businessFinance!=null){
                businessFinance=businessFinance.stream().filter(v->v.getStatus() ==1).collect(Collectors.toList());
                businessFinance.forEach(v->{
                    businessMapper.insert(v);
                });
            }

            if(total!=0){
                isnetEndAll(pageNum,pageSize,token);
            }

        }catch (Exception e){

        }

    }
}
